package cn.itsource.hrm.controller;

import cn.hutool.core.util.ZipUtil;
import cn.itsource.hrm.client.FileClient;
import cn.itsource.hrm.util.AjaxResult;
import cn.itsource.hrm.util.VelocityUtils;
import feign.Response;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;


import java.io.*;
import java.nio.file.Files;
import java.util.Map;

/**
 * @author jinhui
 * @version V1.0.0
 * @date 2020/6/1
 */
@RestController
public class StaticPageController {
    //从配置文件中获取下载路径
    @Value("${template.path}")
    private String path;

    @Autowired
    private FileClient fileClient;

    @PostMapping("/staticPage")
    public AjaxResult staticPage(@RequestParam("fileId") String fileId, @RequestBody Map<String, Object> data) {
        //将模板下载下来

        InputStream inputStream = null;
        OutputStream outputStream = null;
        InputStream input = null;
        OutputStream os = null;
        try {
            Response response = fileClient.download(fileId);
            inputStream = response.body().asInputStream();
            outputStream = new FileOutputStream(path + "static.zip");
            IOUtils.copy(inputStream, outputStream);
            //对模板进行解压,解压到当前目录
            ZipUtil.unzip(path+"static.zip",path);
            String templatePath="E:/templates/static/home.vm";
            String targetPath=path+"home.html";
            String s="E:/templates/static/";
            //添加一个模板根路径的数据，方便再模板中使用
            data.put("staticRoot",s);
            VelocityUtils.staticByTemplate(data,templatePath,targetPath);
            //将静态化后的模板上传到fastdfs中
            File f = new File(path+"home.html");
            FileItem fileItem = new DiskFileItem("file", Files.probeContentType(f.toPath()), false, f.getName(), (int) f.length(), f.getParentFile());
            input = new FileInputStream(f);
            os = fileItem.getOutputStream();
            IOUtils.copy(input, os);
            input.close();
            os.close();
            MultipartFile multipartFile = new CommonsMultipartFile(fileItem);
            AjaxResult result = fileClient.upload(multipartFile);
            return AjaxResult.me().setSuccess(true).setMessage("静态化成功").setResultObj(result.getResultObj());
        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("静态化失败"+e.getMessage());
        } finally {
            try {
                if (outputStream != null) {
                    outputStream.close();
                }
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
